package com.heo.protocol.packet;

import org.apache.log4j.Logger;
import com.heo.util.ByteHelper;

/**
 * 服务器主动下发，给设备发启动指令
 * 
 * @author chichi
 */
public class StartDeviceReq extends BasePacket
{
  private int BODY_LENGTH = -1;// 包体长未知
  private static final String PACKET_NAME = "[启动设备请求包]";

  private static Logger log = Logger.getLogger(StartDeviceReq.class);

  private int channelIndex;// 通道编号，2个字节
  private byte[] startPara;// 启动参数，字节数组
  private String sendJson;//发送指令

  /**
   * 通过网络传过来的流构建对象
   * 
   * @param totalLength
   * @param commandId
   * @param sequenceId
   * @param body
   * @throws Exception
   */
  public StartDeviceReq(String logHeader, SocketPacket packet) throws Exception
  {
    super.copyPacket(logHeader, PACKET_NAME, packet);
    makeBody();
  }

  @Override
  public int getTotalLength()
  {
    return super.HEAD_LENGTH + 2 + startPara.length;
  }

  public StartDeviceReq(int channelIndex, byte[] startPara, int sequenceId) throws Exception
  {
    super(PACKET_NAME);
    this.channelIndex = channelIndex;
    this.startPara = startPara;
    super.sequenceId = sequenceId;
    super.totalLength = (short) (HEAD_LENGTH + 2 + startPara.length);
    super.commandId = DeviceCommand.START_DEVICE_REQ;
    super.sendCount = 1;
  }
  
  public StartDeviceReq(int channelIndex, byte[] startPara, int sequenceId, String sendJson) throws Exception
  {
    super(PACKET_NAME);
    this.channelIndex = channelIndex;
    this.sendJson = sendJson;
    this.startPara = startPara;
    super.sequenceId = sequenceId;
    super.totalLength = (short) (HEAD_LENGTH + 2 + startPara.length);
    super.commandId = DeviceCommand.START_DEVICE_REQ;
    super.sendCount = 1;
  }

  @Override
  public byte[] getPacketBody()
  {
    return ByteHelper.margeByte(ByteHelper.shortToByteArray((short) channelIndex), startPara, null, null, null, null, null, null, null, null);
  }

  @Override
  public void makeBody() throws Exception
  {
    BODY_LENGTH = packetBody.length;
    if (packetBody.length != BODY_LENGTH)
    {
      log.error(super.logHeader + super.packetName + "数据包长度错误。包体长度应该为：" + BODY_LENGTH + "个字节。");
      throw new Exception(super.logHeader + super.packetName + "数据包长度错误。包体长度应该为：" + BODY_LENGTH + "个字节。");
    }
    channelIndex = ByteHelper.byteToInt(packetBody, 0, 2);
    startPara = ByteHelper.toSubByteArray(packetBody, 2, packetBody.length - 2);
    log.debug(super.logHeader + super.packetName + "数据包解析成功。");
  }

  public static void main(String[] args)
  {

  }

  public int getChannelIndex()
  {
    return channelIndex;
  }

  public void setChannelIndex(int channelIndex)
  {
    this.channelIndex = channelIndex;
  }

  public byte[] getStartPara()
  {
    return startPara;
  }

  public void setStartPara(byte[] startPara)
  {
    this.startPara = startPara;
  }

public String getSendJson() {
	return sendJson;
}

public void setSendJson(String sendJson) {
	this.sendJson = sendJson;
}

}
